<template>
  <div>
    <top @sendCollapse="getCollapse" :memberVo="memberVo"></top>
    <left-menu :isCollapse="isCollapse"></left-menu>
    <div class="content-inner" :class="{'active' : isCollapse}">
      <div class="container-fluid">
        <transition name="el-fade-in-linear">
          <router-view/>
        </transition>
      </div>
    </div>
  </div>
</template>
<script>
import Top from "common/Top";
import LeftMenu from "common/Left";
import FooterPage from "common/Footer";
import MainPage from "common/Main";

import { getInfoByToken } from "api/MemberApi";
import { findPermissionByMemberId } from "api/PermissionApi";

import {mapMutations, mapState} from 'vuex'

export default {
  name: "Home",
  components: {
    Top,
    FooterPage,
    LeftMenu,
    MainPage
  },
  data() {
    return {
      isCollapse: false,
      memberVo: {}
    };
  },
  computed: {
    ...mapState(['refreshPage'])
  },
  methods: {
    getCollapse(isCollapse) {
      this.isCollapse = isCollapse;
    },
    getInfoByToken() {
      getInfoByToken().then(res => {
        if (res) {
          this.memberVo = res;
        }
      });
    },
    findPermissionByMemberId() {
      findPermissionByMemberId().then(res => {
        if (res) {
          this.savePermissionList(res);

          if(!this.refreshPage) {
            this.validateThisHome(res)
          }
        }
      });
    },
    validateThisHome (items) {
      if (items != null && items.length != 0) {
        items.forEach(item => {
          if(item.isHome == 0) {
            this.validateThisHome(item.childs)
          } else {
            this.saveRefreshPage(2)
            this.saveHomePage(item.permissionUrl)
            this.$router.replace(item.permissionUrl)
          }
        });
      }
    },
    ...mapMutations(['savePermissionList', 'saveRefreshPage', 'saveHomePage'])
  },
  created() {
    this.getInfoByToken();
    this.findPermissionByMemberId();
  }
};
</script>

<style lang="stylus" scoped>
@import '~assets/css/style.styl';

.content-inner {
  width: 'calc(100% - %s)' % $side-navbar-large-width;
  margin-left: $side-navbar-large-width;
  padding: 0;
  styl_transition();
}

.content-inner.active {
  width: 'calc(100% - %s)' % $side-navbar-small-width;
  margin-left: $side-navbar-small-width;
}
</style>


